package com.team.panicgame.view.mauricio;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.team.panicgame.GameConstants;
import com.team.panicgame.models.mauricio.Frog;
import com.team.panicgame.models.mauricio.Ladder;
import com.team.panicgame.models.mauricio.MauricioWorld;

public class MauricioRenderer {
	private MauricioWorld world;
	private OrthographicCamera camera;
	private ShapeRenderer shapeRenderer = new ShapeRenderer();
	private SpriteBatch spriteBatch = new SpriteBatch();
	private FPSLogger fpsLogger = new FPSLogger();
	
	// testing the movement of the camera
	private Array<Vector2> cameraPositions;
	private int totalCameras = 5;
	private int translateSteps = 120;			// 2s = 120 frames
	private int cameraIndex = 0;
	private int currentStep = 0;
	
	public MauricioRenderer(){
		loadResources();
		
		// set the camera positions
		cameraPositions = new Array<Vector2>();
		cameraPositions.add(new Vector2(
			GameConstants.GAME_WIDTH / 2f,
			GameConstants.GAME_HEIGHT / 2f
		));		
		for (int i = 0; i < totalCameras; i += 1) {
			cameraPositions.add(new Vector2(
				GameConstants.GAME_WIDTH / 2f + (0.5f - (float)Math.random()) * 10f,
				GameConstants.GAME_HEIGHT / 2f + (0.5f - (float)Math.random()) * 10f
			));
		}
		
		// set the camera
		camera = new OrthographicCamera(GameConstants.GAME_WIDTH, GameConstants.GAME_HEIGHT);
		camera.position.x = GameConstants.GAME_WIDTH / 2f;
		camera.position.y = GameConstants.GAME_HEIGHT / 2f;
		camera.update();
	}
	
	public void loadResources(){
		loadTextures();
		loadSounds();
	}
	
	private void loadTextures(){
	}
	private void loadSounds(){
	}
	
	public void resize(float width,float height){
	}
	
	public void setWorld(MauricioWorld world){
		this.world = world;
	}
	
	public void render(){
		initCamera();
		updateCamera();
		renderFrog(world.frog);
		renderLadders(world.ladders);
	}
	
	public void renderFrog(Frog frog) {
		shapeRenderer.begin(ShapeType.Box);
			shapeRenderer.setColor(1f, 1f, 1f, 1f);
			shapeRenderer.box(frog.shape.x, frog.shape.y, 0f, frog.shape.width, frog.shape.height, 0f);
		shapeRenderer.end();
	}
	
	public void renderLadders(ArrayList<Ladder> ladders) {
		shapeRenderer.begin(ShapeType.Box);
			for (int i = 0; i < ladders.size(); i++) {
				Ladder ladder = ladders.get(i);
				shapeRenderer.setColor(1f, 1f, 1f, 1f);
				shapeRenderer.box(ladder.shape.x, ladder.shape.y, 0f, ladder.shape.width, ladder.shape.height, 0f);
			}
		shapeRenderer.end();
	}
	
	public void initCamera(){
		shapeRenderer.setProjectionMatrix(camera.combined);
		spriteBatch.getProjectionMatrix().setToOrtho2D(0f, 0, GameConstants.GAME_WIDTH, GameConstants.GAME_HEIGHT);
		
		Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		
		fpsLogger.log();
	}
	
	public void updateCamera() {
		
		// test cameras
//		if (currentStep == translateSteps) {
//			currentStep = 0;
//			camera.position.x = cameraPositions.get(cameraIndex).x;
//			camera.position.y = cameraPositions.get(cameraIndex).y;
//			camera.update();
//			cameraIndex = (cameraIndex + 1) % totalCameras;
//		}
//		currentStep += 1;
		// end test
		
		// follow the frog O:
		camera.position.x = world.frog.shape.x + world.frog.shape.width / 2f;
		camera.position.y = world.frog.shape.y + world.frog.shape.height / 2f;
		camera.update();
	}
}